<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <script>

        n=5
        version = 4
        function isBadVersion(ver) {
            return (ver>=version)? true : false
        }
        // 思路：类似二分法
        // 从(max-min)/2取值mid，如果是错误版本 (isBadVersion(mid)==true) 取(max-min)/2继续验证
        // 如果不是错误版本 (isBadVersion(m)==false) 取 (n+m)/2 继续验证
        var solution = function(isBadVersion) {
            /**
             * @param {integer} n Total versions
             * @return {integer} The first bad version
             */
            return function(n) {
                let max = n, min = 0, mid = 0
                while (min<max-1){
                    mid = Math.floor((max+min)/2)
                    if (isBadVersion(mid)) {
                        max = mid
                    }else {
                        min = mid
                    }
                }
                return max
            };
        };

        var fn1 = solution(isBadVersion)
        fn1(n)

    </script>
</body>
</html>